package com.agile;

public class Scorer {
    private int ball;
    private int[] itsTrows = new int[21];
    private int itsTurrentThrow = 0;

    public void  add(int pins)
    {
        itsTrows[itsTurrentThrow++] = pins;
    }
    public int scoreForFrame(int theFrame)
    {
        ball = 0;
        int score = 0;
        for (int currentFrame = 0; currentFrame < theFrame; currentFrame++)
        {

//            firstThrow = itsTrows[ball++];
//            if (firstThrow == 10)
//            {
//                score += 10 + itsTrows[ball] + itsTrows[ball+1];
//            }else {
//               score += handleSecondThrow();
//            }

            if (strike())
            {
                score += 10 + nextTwoBallsForeStrike();
                ball++;
            }
            else if (spare())
            {
                score += 10 + newBallsForSpare();
                ball += 2;
            }
            else
            {
                score += twoBallsInFrame();
                ball += 2;
            }

        }
        return score;
    }

    public boolean strike()
    {
        return itsTrows[ball] == 10;
    }

    public boolean spare()
    {
        return (itsTrows[ball] + itsTrows[ball+1]) == 10;
    }


    public int nextTwoBallsForeStrike()
    {
        return  itsTrows[ball+1] + itsTrows[ball+2];
    }

    public int newBallsForSpare()
    {
        return itsTrows[ball+2];
    }

    public int twoBallsInFrame()
    {
        return itsTrows[ball]+itsTrows[ball+1];
    }

}
